package com.surfacing.rcp.client.sortAndInfo;

import java.sql.Date;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;

import com.surfacing.rcp.model.common.ITransportStatus;
import com.surfacing.rcp.model.entity.Journal;

/**
 * Provides sorting functions for the journal tables.
 * 
 * @author b.thek
 */
public class JournalSorter extends ViewerSorter implements ITransportStatus {
	private static final Logger log = Logger.getLogger(JournalSorter.class);

	// SELECT j.protokollnummer, j.datum, j.name, tpz.transportziel,
	// j.alarmZeit, j.endZeit, j.created, j.changed, j.notfallort,
	// j.nachforderung, ea.einsatzart, ds.dienststelle, j.azubi,
	// CONCAT(n.firstName,''n.lastName) AS NFS, CONCAT(n.firstName,''n.lastName)
	// AS Arzt, j.naca, j.km \

	public final static String PROTNR_SORTER = "protokollnummer";
	public final static String DATUM_SORTER = "datum";
	public final static String PATIENT_SORTER = "name";
	public final static String TPZ_SORTER = "transportziel";
	public final static String ALARMZEIT_SORTER = "alarmZeit";
	public final static String ENDZEIT_SORTER = "endZeit";
	public final static String CREATED_SORTER = "created";
	public final static String CHANGED_SORTER = "changed";
	public final static String NOTFALLORT_SORTER = "notfallort";
	public final static String NACHFORDERUNG_SORTER = "nachforderung";
	public final static String EINSATZART_SORTER = "einsatzart";
	public final static String DIENSTSTELLE_SORTER = "dienststelle";
	public final static String AZUBI_SORTER = "azubi";
	public final static String NFS_SORTER = "nfs";
	public final static String DOCTOR_SORTER = "doctor";
	public final static String NACA_SORTER = "naca";
	public final static String KM_SORTER = "km";

	// sort the data based on column and direction

	// column to sort
	private String column = null;
	private int dir = SWT.DOWN;

	/**
	 * Default class constructor providing a column to sort and a direction
	 * 
	 * @param column
	 *            the column to sort by
	 * @param dir
	 *            the sorting direction
	 */
	public JournalSorter(String column, int dir) {
		super();
		this.column = column;
		this.dir = dir;
	}

	/**
	 * Compares the given object and returns the result of the comparator
	 * 
	 * @param viewer
	 *            the viewer containing the data
	 * @param object1
	 *            the first object to compare
	 * @param object2
	 *            the second object to compare+
	 * @return the result of the comparation
	 */
	@Override
	public int compare(Viewer viewer, Object object1, Object object2) {
		// cast to a journal
		Journal journal1 = (Journal) object1;
		Journal journal2 = (Journal) object2;
		// the sort direction
		int sortDir = 1;

		if (dir == SWT.DOWN)
			sortDir = -1;

		// sort by the journal number
		if (column == PROTNR_SORTER) {
			if (journal1.getProtokollnummer() == null)
				return -1 * sortDir;
			if (journal2.getProtokollnummer() == null)
				return 1 * sortDir;
			// now compare
			try {
				long term1 = Long.parseLong(journal1.getProtokollnummer());
				long term2 = Long.parseLong(journal2.getProtokollnummer());
				if (term1 > term2)
					return -1 * sortDir;
				if (term1 < term2)
					return 1 * sortDir;
				if (term1 == term2)
					return 0 * sortDir;
			} catch (NumberFormatException ex) {
				log.error(
						"Couldn't parse String to Long (Protokollnummer) because: ",
						ex);

				if (journal1.getId() == 0)
					return -1 * sortDir;
				if (journal2.getId() == 0)
					return 1 * sortDir;
				if (journal1.getId() > journal2.getId())
					return -1 * sortDir;
				if (journal1.getId() < journal2.getId())
					return 1 * sortDir;
				if (journal1.getId() == journal2.getId())
					return 0 * sortDir;
			}
		}
		// sort by the priority of the journal
		if (column == DATUM_SORTER) {
			// assert valid
			Date date1 = journal1.getDatum();
			Date date2 = journal2.getDatum();
			if (date1.compareTo(date2) == -1)
				return -1 * sortDir;
			if (date1.compareTo(date2) == 1)
				return 1 * sortDir;
			if (date1.compareTo(date2) == 0)
				return 0 * sortDir;
		}
		if (column == PATIENT_SORTER) {
			// assert valid
			if (journal1.getName() == null)
				return -1 * sortDir;
			if (journal2.getName() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getName();
			String from2 = journal2.getName();
			return from1.compareTo(from2) * sortDir;
		}
		// sort by the journal from column
		if (column == TPZ_SORTER) {
			// assert valid
			if (journal1.getTransportziel().getTransportziel() == null)
				return -1 * sortDir;
			if (journal2.getTransportziel().getTransportziel() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getTransportziel().getTransportziel();
			String from2 = journal2.getTransportziel().getTransportziel();
			return from1.compareTo(from2) * sortDir;
		}
		// sort by the patient last name
		if (column == ALARMZEIT_SORTER) {
			long term1 = journal1.getAlarmZeit();
			long term2 = journal2.getAlarmZeit();
			if (term1 > term2)
				return -1 * sortDir;
			if (term1 < term2)
				return 1 * sortDir;
			if (term1 == term2)
				return 0 * sortDir;
		}

		// sort by the journal to column
		if (column == ENDZEIT_SORTER) {
			// assert valid
			long term1 = journal1.getEndZeit();
			long term2 = journal2.getEndZeit();
			if (term1 > term2)
				return -1 * sortDir;
			if (term1 < term2)
				return 1 * sortDir;
			if (term1 == term2)
				return 0 * sortDir;
		}

		// sort by the kind of illness
		if (column == CREATED_SORTER) {
			long term1 = journal1.getCreated().getTime();
			long term2 = journal2.getCreated().getTime();
			if (term1 > term2)
				return -1 * sortDir;
			if (term1 < term2)
				return 1 * sortDir;
			if (term1 == term2)
				return 0 * sortDir;
		}

		if (column == CHANGED_SORTER) {
			long term1 = journal1.getChanged().getTime();
			long term2 = journal2.getChanged().getTime();
			if (term1 > term2)
				return -1 * sortDir;
			if (term1 < term2)
				return 1 * sortDir;
			if (term1 == term2)
				return 0 * sortDir;
		}

		// sort by the time of the 'S1' field
		if (column == NOTFALLORT_SORTER) {
			if (journal1.getNotfallort() == null)
				return -1 * sortDir;
			if (journal2.getNotfallort() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getNotfallort();
			String from2 = journal2.getNotfallort();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S2' field
		if (column == NACHFORDERUNG_SORTER) {
			if (journal1.getNachforderung().getAnforderung() == null)
				return -1 * sortDir;
			if (journal2.getNachforderung().getAnforderung() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getNachforderung().getAnforderung();
			String from2 = journal2.getNachforderung().getAnforderung();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S3' field
		if (column == EINSATZART_SORTER) {
			if (journal1.getEinsatzart().getEinsatzart() == null)
				return -1 * sortDir;
			if (journal2.getEinsatzart().getEinsatzart() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getEinsatzart().getEinsatzart();
			String from2 = journal2.getEinsatzart().getEinsatzart();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S4' field
		if (column == DIENSTSTELLE_SORTER) {
			if (journal1.getDienststelle().getDienststelle() == null)
				return -1 * sortDir;
			if (journal2.getDienststelle().getDienststelle() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getDienststelle().getDienststelle();
			String from2 = journal2.getDienststelle().getDienststelle();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S5' field
		if (column == AZUBI_SORTER) {
			if (journal1.getAzubi() == null)
				return -1 * sortDir;
			if (journal2.getAzubi() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getAzubi();
			String from2 = journal2.getAzubi();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S6' field
		if (column == NFS_SORTER) {
			if (journal1.getNfs().getLastName() == null)
				return -1 * sortDir;
			if (journal2.getNfs().getLastName() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getNfs().getLastName();
			String from2 = journal2.getNfs().getLastName();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S7' field
		if (column == DOCTOR_SORTER) {
			if (journal1.getDoctor().getLastName() == null)
				return -1 * sortDir;
			if (journal2.getDoctor().getLastName() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getDoctor().getLastName();
			String from2 = journal2.getDoctor().getLastName();
			return from1.compareTo(from2) * sortDir;
		}

		// sort by the time of the 'S8' field
		if (column == NACA_SORTER) {
			int number1 = journal1.getNaca().getNaca();
			int number2 = journal2.getNaca().getNaca();
			if (number1 > number2)
				return -1 * sortDir;
			if (number1 < number2)
				return 1 * sortDir;
			if (number1 == number2)
				return 0 * sortDir;
		}

		// sort by the time of the 'S9' field
		if (column == KM_SORTER) {
			if (journal1.getKm() == null)
				return -1 * sortDir;
			if (journal2.getKm() == null)
				return 1 * sortDir;
			// now compare
			String from1 = journal1.getKm();
			String from2 = journal2.getKm();
			return from1.compareTo(from2) * sortDir;
		}

		return 0;
	}
}
